From 4ef8fd300ab3e33ddad990843da8384cf68a7b1e Mon Sep 17 00:00:00 2001 From: "Wolfgang (Wolle) Ewald" Date: Thu, 18 Feb 2021 23:25:16 +0100 Subject: Add files via upload --- src/ADS1115_WE.cpp | 326 ++++++++++++++++++++++++++++------------------ src/ADS1115_WE.h | 374 ++++++++++++++++++++++++++++------------------------- 2 files changed, 397 insertions(+), 303 deletions(-) diff --git a/src/ADS1115_WE.cpp b/src/ADS1115_WE.cpp index e771f08..d9547ad 100644 --- a/src/ADS1115_WE.cpp +++ b/src/ADS1115_WE.cpp @@ -17,193 +17,271 @@ #include "ADS1115_WE.h" ADS1115_WE::ADS1115_WE(int addr){ - i2cAddress = addr; + i2cAddress = addr; } ADS1115_WE::ADS1115_WE(){ - i2cAddress = 0x48; + i2cAddress = 0x48; } void ADS1115_WE::reset(){ - Wire.beginTransmission(0); - Wire.write(0x06); - Wire.endTransmission(); + Wire.beginTransmission(0); + Wire.write(0x06); + Wire.endTransmission(); } -bool ADS1115_WE::init(){ - Wire.beginTransmission(i2cAddress); - uint8_t success = Wire.endTransmission(); - if(success){ - return 0; - } - writeRegister(ADS1115_CONFIG_REG, ADS1115_REG_RESET_VAL); - setVoltageRange_mV(ADS1115_RANGE_2048); - writeRegister(ADS1115_LO_THRESH_REG, 0x8000); - writeRegister(ADS1115_HI_THRESH_REG, 0x7FFF); - return 1; +bool ADS1115_WE::init(){ + Wire.beginTransmission(i2cAddress); + uint8_t success = Wire.endTransmission(); + if(success){ + return 0; + } + writeRegister(ADS1115_CONFIG_REG, ADS1115_REG_RESET_VAL); + setVoltageRange_mV(ADS1115_RANGE_2048); + writeRegister(ADS1115_LO_THRESH_REG, 0x8000); + writeRegister(ADS1115_HI_THRESH_REG, 0x7FFF); + deviceMeasureMode = ADS1115_SINGLE; + return 1; } void ADS1115_WE::setAlertPinMode(ADS1115_COMP_QUE mode){ - uint16_t currentConfReg = readRegister(ADS1115_CONFIG_REG); - currentConfReg &= ~(0x0003); - currentConfReg |= mode; - writeRegister(ADS1115_CONFIG_REG, currentConfReg); + uint16_t currentConfReg = readRegister(ADS1115_CONFIG_REG); + currentConfReg &= ~(0x0003); + currentConfReg |= mode; + writeRegister(ADS1115_CONFIG_REG, currentConfReg); } void ADS1115_WE::setAlertLatch(ADS1115_LATCH latch){ - uint16_t currentConfReg = readRegister(ADS1115_CONFIG_REG); - currentConfReg &= ~(0x0004); - currentConfReg |= latch; - writeRegister(ADS1115_CONFIG_REG, currentConfReg); + uint16_t currentConfReg = readRegister(ADS1115_CONFIG_REG); + currentConfReg &= ~(0x0004); + currentConfReg |= latch; + writeRegister(ADS1115_CONFIG_REG, currentConfReg); } void ADS1115_WE::setAlertPol(ADS1115_ALERT_POL polarity){ - uint16_t currentConfReg = readRegister(ADS1115_CONFIG_REG); - currentConfReg &= ~(0x0008); - currentConfReg |= polarity; - writeRegister(ADS1115_CONFIG_REG, currentConfReg); + uint16_t currentConfReg = readRegister(ADS1115_CONFIG_REG); + currentConfReg &= ~(0x0008); + currentConfReg |= polarity; + writeRegister(ADS1115_CONFIG_REG, currentConfReg); } void ADS1115_WE::setAlertModeAndLimit_V(ADS1115_COMP_MODE mode, float hiThres, float loThres){ - uint16_t currentConfReg = readRegister(ADS1115_CONFIG_REG); - currentConfReg &= ~(0x0010); - currentConfReg |= mode; - writeRegister(ADS1115_CONFIG_REG, currentConfReg); - int16_t alertLimit = calcLimit(hiThres); - writeRegister(ADS1115_HI_THRESH_REG, alertLimit); - alertLimit = calcLimit(loThres); - writeRegister(ADS1115_LO_THRESH_REG, alertLimit); - + uint16_t currentConfReg = readRegister(ADS1115_CONFIG_REG); + currentConfReg &= ~(0x0010); + currentConfReg |= mode; + writeRegister(ADS1115_CONFIG_REG, currentConfReg); + int16_t alertLimit = calcLimit(hiThres); + writeRegister(ADS1115_HI_THRESH_REG, alertLimit); + alertLimit = calcLimit(loThres); + writeRegister(ADS1115_LO_THRESH_REG, alertLimit); + } void ADS1115_WE::setConvRate(ADS1115_CONV_RATE rate){ - uint16_t currentConfReg = readRegister(ADS1115_CONFIG_REG); - currentConfReg &= ~(0x00E0); - currentConfReg |= rate; - writeRegister(ADS1115_CONFIG_REG, currentConfReg); + uint16_t currentConfReg = readRegister(ADS1115_CONFIG_REG); + currentConfReg &= ~(0x00E0); + currentConfReg |= rate; + writeRegister(ADS1115_CONFIG_REG, currentConfReg); } - + +convRate ADS1115_WE::getConvRate(){ + uint16_t currentConfReg = readRegister(ADS1115_CONFIG_REG); + return (convRate)(currentConfReg & 0xE0); +} + void ADS1115_WE::setMeasureMode(ADS1115_MEASURE_MODE mode){ - uint16_t currentConfReg = readRegister(ADS1115_CONFIG_REG); - currentConfReg &= ~(0x0100); - currentConfReg |= mode; - writeRegister(ADS1115_CONFIG_REG, currentConfReg); + uint16_t currentConfReg = readRegister(ADS1115_CONFIG_REG); + deviceMeasureMode = mode; + currentConfReg &= ~(0x0100); + currentConfReg |= mode; + writeRegister(ADS1115_CONFIG_REG, currentConfReg); } void ADS1115_WE::setVoltageRange_mV(ADS1115_RANGE range){ - uint16_t currentVoltageRange = voltageRange; - uint16_t currentConfReg = readRegister(ADS1115_CONFIG_REG); - uint16_t currentRange = (currentConfReg >> 9) & 7; - uint16_t currentAlertPinMode = currentConfReg & 3; - - setMeasureMode(ADS1115_SINGLE); - - switch(range){ - case ADS1115_RANGE_6144: - voltageRange = 6144; - break; - case ADS1115_RANGE_4096: - voltageRange = 4096; - break; - case ADS1115_RANGE_2048: - voltageRange = 2048; - break; - case ADS1115_RANGE_1024: - voltageRange = 1024; - break; - case ADS1115_RANGE_0512: - voltageRange = 512; - break; - case ADS1115_RANGE_0256: - voltageRange = 256; - break; - } - - if ((currentRange != range) && (currentAlertPinMode != ADS1115_DISABLE_ALERT)){ - int16_t alertLimit = readRegister(ADS1115_HI_THRESH_REG); - alertLimit = alertLimit * (currentVoltageRange * 1.0 / voltageRange); - writeRegister(ADS1115_HI_THRESH_REG, alertLimit); - - alertLimit = readRegister(ADS1115_LO_THRESH_REG); - alertLimit = alertLimit * (currentVoltageRange * 1.0 / voltageRange); - writeRegister(ADS1115_LO_THRESH_REG, alertLimit); - } - - currentConfReg &= ~(0x0E00); - currentConfReg |= range; - writeRegister(ADS1115_CONFIG_REG, currentConfReg); + uint16_t currentVoltageRange = voltageRange; + uint16_t currentConfReg = readRegister(ADS1115_CONFIG_REG); + uint16_t currentRange = (currentConfReg >> 9) & 7; + uint16_t currentAlertPinMode = currentConfReg & 3; + + setMeasureMode(ADS1115_SINGLE); + + switch(range){ + case ADS1115_RANGE_6144: + voltageRange = 6144; + break; + case ADS1115_RANGE_4096: + voltageRange = 4096; + break; + case ADS1115_RANGE_2048: + voltageRange = 2048; + break; + case ADS1115_RANGE_1024: + voltageRange = 1024; + break; + case ADS1115_RANGE_0512: + voltageRange = 512; + break; + case ADS1115_RANGE_0256: + voltageRange = 256; + break; + } + + if ((currentRange != range) && (currentAlertPinMode != ADS1115_DISABLE_ALERT)){ + int16_t alertLimit = readRegister(ADS1115_HI_THRESH_REG); + alertLimit = alertLimit * (currentVoltageRange * 1.0 / voltageRange); + writeRegister(ADS1115_HI_THRESH_REG, alertLimit); + + alertLimit = readRegister(ADS1115_LO_THRESH_REG); + alertLimit = alertLimit * (currentVoltageRange * 1.0 / voltageRange); + writeRegister(ADS1115_LO_THRESH_REG, alertLimit); + } + + currentConfReg &= ~(0x0E00); + currentConfReg |= range; + writeRegister(ADS1115_CONFIG_REG, currentConfReg); +} + +void ADS1115_WE::setAutoRange(){ + uint16_t currentConfReg = readRegister(ADS1115_CONFIG_REG); + convRate rate = getConvRate(); + setVoltageRange_mV(ADS1115_RANGE_6144); + delayAccToRate(rate); + + if(deviceMeasureMode == ADS1115_SINGLE){ + setMeasureMode(ADS1115_CONTINUOUS); + delayAccToRate(rate); + } + + float result = abs(getResult_mV()); + range optRange = ADS1115_RANGE_6144; + + if(result < 205.0){ + optRange = ADS1115_RANGE_0256; + } + else if(result < 410.0){ + optRange = ADS1115_RANGE_0512; + } + else if(result < 820.0){ + optRange = ADS1115_RANGE_1024; + } + else if(result < 1640.0){ + optRange = ADS1115_RANGE_2048; + } + else if(result < 3280.0){ + optRange = ADS1115_RANGE_4096; + } + + writeRegister(ADS1115_CONFIG_REG, currentConfReg); + setVoltageRange_mV(optRange); + delayAccToRate(rate); +} + +void ADS1115_WE::delayAccToRate(convRate cr){ + switch(cr){ + case ADS1115_8_SPS: + delay(130); + break; + case ADS1115_16_SPS: + delay(65); + break; + case ADS1115_32_SPS: + delay(32); + break; + case ADS1115_64_SPS: + delay(16); + break; + case ADS1115_128_SPS: + delay(8); + break; + case ADS1115_250_SPS: + delay(4); + break; + case ADS1115_475_SPS: + delay(3); + break; + case ADS1115_860_SPS: + delay(2); + break; + } } + void ADS1115_WE::setCompareChannels(ADS1115_MUX mux){ - uint16_t currentConfReg = readRegister(ADS1115_CONFIG_REG); - currentConfReg &= ~(0x7000); - currentConfReg |= (mux); - writeRegister(ADS1115_CONFIG_REG, currentConfReg); + uint16_t currentConfReg = readRegister(ADS1115_CONFIG_REG); + currentConfReg &= ~(0x7000); + currentConfReg |= (mux); + writeRegister(ADS1115_CONFIG_REG, currentConfReg); + + if(!(currentConfReg & 0x0100)){ // => if not single shot mode + convRate rate = getConvRate(); + delayAccToRate(rate); + delayAccToRate(rate); + } } - + bool ADS1115_WE::isBusy(){ - uint16_t currentConfReg = readRegister(ADS1115_CONFIG_REG); - return (!(currentConfReg>>15) & 1); + uint16_t currentConfReg = readRegister(ADS1115_CONFIG_REG); + return (!(currentConfReg>>15) & 1); } - + void ADS1115_WE::startSingleMeasurement(){ - uint16_t currentConfReg = readRegister(ADS1115_CONFIG_REG); - currentConfReg |= (1 << 15); - writeRegister(ADS1115_CONFIG_REG, currentConfReg); + uint16_t currentConfReg = readRegister(ADS1115_CONFIG_REG); + currentConfReg |= (1 << 15); + writeRegister(ADS1115_CONFIG_REG, currentConfReg); } - + float ADS1115_WE::getResult_V(){ - int16_t rawResult = readRegister(ADS1115_CONV_REG); - float result = (rawResult * 1.0 / ADS1115_REG_FACTOR) * voltageRange/1000; - return result; + int16_t rawResult = readRegister(ADS1115_CONV_REG); + float result = (rawResult * 1.0 / ADS1115_REG_FACTOR) * voltageRange/1000; + return result; } float ADS1115_WE::getResult_mV(){ - int16_t rawResult = readRegister(ADS1115_CONV_REG); - float result = (rawResult * 1.0 / ADS1115_REG_FACTOR) * voltageRange; - return result; + int16_t rawResult = readRegister(ADS1115_CONV_REG); + float result = (rawResult * 1.0 / ADS1115_REG_FACTOR) * voltageRange; + return result; } int16_t ADS1115_WE::getRawResult(){ - int16_t rawResult = readRegister(ADS1115_CONV_REG); - return rawResult; + int16_t rawResult = readRegister(ADS1115_CONV_REG); + return rawResult; } int16_t ADS1115_WE::getResultWithRange(int16_t min, int16_t max){ - int16_t rawResult = readRegister(ADS1115_CONV_REG); - int16_t result = 0; - result = map(rawResult, -32767, 32767, min, max); - return result; + int16_t rawResult = readRegister(ADS1115_CONV_REG); + int16_t result = 0; + result = map(rawResult, -32767, 32767, min, max); + return result; } int16_t ADS1115_WE::getResultWithRange(int16_t min, int16_t max, int16_t maxMillivolt){ - int16_t rawResult = readRegister(ADS1115_CONV_REG); - int16_t result = 0; - result = map(rawResult, -32767, 32767, min, max); - result = (int16_t) ((1.0 * result * voltageRange / maxMillivolt) + 0.5); - return result; + int16_t rawResult = readRegister(ADS1115_CONV_REG); + int16_t result = 0; + result = map(rawResult, -32767, 32767, min, max); + result = (int16_t) ((1.0 * result * voltageRange / maxMillivolt) + 0.5); + return result; } uint16_t ADS1115_WE::getVoltageRange_mV(){ - return voltageRange; + return voltageRange; } void ADS1115_WE::setAlertPinToConversionReady(){ - writeRegister(ADS1115_LO_THRESH_REG, (0<<15)); - writeRegister(ADS1115_HI_THRESH_REG, (1<<15)); + writeRegister(ADS1115_LO_THRESH_REG, (0<<15)); + writeRegister(ADS1115_HI_THRESH_REG, (1<<15)); } void ADS1115_WE::clearAlert(){ - readRegister(ADS1115_CONV_REG); + readRegister(ADS1115_CONV_REG); } /************************************************ - private functions + private functions *************************************************/ int16_t ADS1115_WE::calcLimit(float rawLimit){ - int16_t limit = (int16_t)((rawLimit * ADS1115_REG_FACTOR / voltageRange)*1000); - return limit; + int16_t limit = (int16_t)((rawLimit * ADS1115_REG_FACTOR / voltageRange)*1000); + return limit; } uint8_t ADS1115_WE::writeRegister(uint8_t reg, uint16_t val){ @@ -230,6 +308,6 @@ uint16_t ADS1115_WE::readRegister(uint8_t reg){ regValue = (MSByte<<8) + LSByte; return regValue; } - + diff --git a/src/ADS1115_WE.h b/src/ADS1115_WE.h index 7fcd4ba..6d68123 100644 --- a/src/ADS1115_WE.h +++ b/src/ADS1115_WE.h @@ -28,224 +28,240 @@ #include /* registers */ -#define ADS1115_CONV_REG 0x00 // Conversion Register -#define ADS1115_CONFIG_REG 0x01 // Configuration Register -#define ADS1115_LO_THRESH_REG 0x02 // Low Threshold Register -#define ADS1115_HI_THRESH_REG 0x03 // High Threshold Register +#define ADS1115_CONV_REG 0x00 // Conversion Register +#define ADS1115_CONFIG_REG 0x01 // Configuration Register +#define ADS1115_LO_THRESH_REG 0x02 // Low Threshold Register +#define ADS1115_HI_THRESH_REG 0x03 // High Threshold Register /* other */ #define ADS1115_REG_FACTOR 32768 #define ADS1115_REG_RESET_VAL 0x8583 typedef enum ADS1115_COMP_QUE { - ADS1115_ASSERT_AFTER_1 = 0x0000, - ADS1115_ASSERT_AFTER_2 = 0x0001, - ADS1115_ASSERT_AFTER_4 = 0x0002, - ADS1115_DISABLE_ALERT = 0x0003 + ADS1115_ASSERT_AFTER_1 = 0x0000, + ADS1115_ASSERT_AFTER_2 = 0x0001, + ADS1115_ASSERT_AFTER_4 = 0x0002, + ADS1115_DISABLE_ALERT = 0x0003 } compQue; typedef enum ADS1115_LATCH { - ADS1115_LATCH_DISABLED = 0x0000, - ADS1115_LATCH_ENABLED = 0x0004, + ADS1115_LATCH_DISABLED = 0x0000, + ADS1115_LATCH_ENABLED = 0x0004, } latch; typedef enum ADS1115_ALERT_POL { - ADS1115_ACT_LOW = 0x0000, - ADS1115_ACT_HIGH = 0x0008 + ADS1115_ACT_LOW = 0x0000, + ADS1115_ACT_HIGH = 0x0008 } alertPol; typedef enum ADS1115_COMP_MODE{ - ADS1115_MAX_LIMIT = 0x0000, - ADS1115_WINDOW = 0x0010 + ADS1115_MAX_LIMIT = 0x0000, + ADS1115_WINDOW = 0x0010 } compMode; typedef enum ADS1115_CONV_RATE{ - ADS1115_8_SPS = 0x0000, - ADS1115_16_SPS = 0x0020, - ADS1115_32_SPS = 0x0040, - ADS1115_64_SPS = 0x0050, - ADS1115_128_SPS = 0x0080, - ADS1115_250_SPS = 0x00A0, - ADS1115_475_SPS = 0x00C0, - ADS1115_860_SPS = 0x00E0 + ADS1115_8_SPS = 0x0000, + ADS1115_16_SPS = 0x0020, + ADS1115_32_SPS = 0x0040, + ADS1115_64_SPS = 0x0060, + ADS1115_128_SPS = 0x0080, + ADS1115_250_SPS = 0x00A0, + ADS1115_475_SPS = 0x00C0, + ADS1115_860_SPS = 0x00E0 } convRate; typedef enum ADS1115_MEASURE_MODE{ - ADS1115_CONTINOUS = 0x0000, // keeping misspelled enum for backwards compatibility. - ADS1115_CONTINUOUS = 0x0000, - ADS1115_SINGLE = 0x0100 + ADS1115_CONTINOUS = 0x0000, // keeping misspelled enum for backwards compatibility. + ADS1115_CONTINUOUS = 0x0000, + ADS1115_SINGLE = 0x0100 } measureMode; typedef enum ADS1115_RANGE{ - ADS1115_RANGE_6144 = 0x0000, - ADS1115_RANGE_4096 = 0x0200, - ADS1115_RANGE_2048 = 0x0400, - ADS1115_RANGE_1024 = 0x0600, - ADS1115_RANGE_0512 = 0x0800, - ADS1115_RANGE_0256 = 0x0A00, + ADS1115_RANGE_6144 = 0x0000, + ADS1115_RANGE_4096 = 0x0200, + ADS1115_RANGE_2048 = 0x0400, + ADS1115_RANGE_1024 = 0x0600, + ADS1115_RANGE_0512 = 0x0800, + ADS1115_RANGE_0256 = 0x0A00, } range; typedef enum ADS1115_MUX{ - ADS1115_COMP_0_1 = 0x0000, - ADS1115_COMP_0_3 = 0x1000, - ADS1115_COMP_1_3 = 0x2000, - ADS1115_COMP_2_3 = 0x3000, - ADS1115_COMP_0_GND = 0x4000, - ADS1115_COMP_1_GND = 0x5000, - ADS1115_COMP_2_GND = 0x6000, - ADS1115_COMP_3_GND = 0x7000 + ADS1115_COMP_0_1 = 0x0000, + ADS1115_COMP_0_3 = 0x1000, + ADS1115_COMP_1_3 = 0x2000, + ADS1115_COMP_2_3 = 0x3000, + ADS1115_COMP_0_GND = 0x4000, + ADS1115_COMP_1_GND = 0x5000, + ADS1115_COMP_2_GND = 0x6000, + ADS1115_COMP_3_GND = 0x7000 } mux; typedef enum ADS1115_STATUS_OR_START{ - ADS1115_BUSY = 0x0000, - ADS1115_START_ISREADY = 0x8000 + ADS1115_BUSY = 0x0000, + ADS1115_START_ISREADY = 0x8000 } statusOrStart; class ADS1115_WE { public: - ADS1115_WE(int addr); - ADS1115_WE(); // uses default I2C Address 0x48 - - void reset(); - bool init(); - - /* Set number of conversions after which the alert pin will be active - * - or you can disable the alert - * - * ADS1115_ASSERT_AFTER_1 -> after 1 conversion - * ADS1115_ASSERT_AFTER_2 -> after 2 conversions - * ADS1115_ASSERT_AFTER_4 -> after 4 conversions - * ADS1115_DISABLE_ALERT -> disable comparator // alert pin (default) - */ - void setAlertPinMode(ADS1115_COMP_QUE mode); - - /* Enable or disable latch. If latch is enabled the alarm pin will be active until the - * conversion register is read (getResult functions). If disabled the alarm pin will be - * deactivated with next value within limits. - * - * ADS1115_LATCH_DISABLED (default) - * ADS1115_LATCH_ENABLED - */ - void setAlertLatch(ADS1115_LATCH latch); - - /* Sets the alert pin polarity if active: - * - * Enable or disable latch. If latch is enabled the alarm pin will be active until the - * conversion register is read (getResult functions). If disabled the alarm pin will be - * deactivated with next value within limits. - * - * ADS1115_ACT_LOW -> active low (default) - * ADS1115_ACT_HIGH -> active high - */ - void setAlertPol(ADS1115_ALERT_POL polarity); - - /* Choose maximum limit or maximum and minimum alert limit (window)in Volt - alert pin will - * be active when measured values are beyond the maximum limit or outside the window - * Upper limit first: setAlertLimit_V(MODE, maximum, minimum) - * In max limit mode the minimum value is the limit where the alert pin will be deactivated (if - * not latched) - * - * ADS1115_MAX_LIMIT - * ADS1115_WINDOW - */ - void setAlertModeAndLimit_V(ADS1115_COMP_MODE mode, float hithres, float lothres); - - /* Set the conversion rate in SPS (samples per second) - * Options should be self-explaining: - * - * ADS1115_8_SPS - * ADS1115_16_SPS - * ADS1115_32_SPS - * ADS1115_64_SPS - * ADS1115_128_SPS (default) - * ADS1115_250_SPS - * ADS1115_475_SPS - * ADS1115_860_SPS - */ - void setConvRate(ADS1115_CONV_RATE rate); - - /* Set continuous or single shot mode: - * - * ADS1115_CONTINUOUS -> continuous mode - * ADS1115_SINGLE -> single shot mode (default) - */ - void setMeasureMode(ADS1115_MEASURE_MODE mode); - - /* Set the voltage range of the ADC to adjust the gain: - * Please note that you must not apply more than VDD + 0.3V to the input pins! - * - * ADS1115_RANGE_6144 -> +/- 6144 mV - * ADS1115_RANGE_4096 -> +/- 4096 mV - * ADS1115_RANGE_2048 -> +/- 2048 mV (default) - * ADS1115_RANGE_1024 -> +/- 1024 mV - * ADS1115_RANGE_0512 -> +/- 512 mV - * ADS1115_RANGE_0256 -> +/- 256 mV - */ - void setVoltageRange_mV(ADS1115_RANGE range); - - /* Set the inputs to be compared - * - * ADS1115_COMP_0_1 -> compares 0 with 1 (default) - * ADS1115_COMP_0_3 -> compares 0 with 3 - * ADS1115_COMP_1_3 -> compares 1 with 3 - * ADS1115_COMP_2_3 -> compares 2 with 3 - * ADS1115_COMP_0_GND -> compares 0 with GND - * ADS1115_COMP_1_GND -> compares 1 with GND - * ADS1115_COMP_2_GND -> compares 2 with GND - * ADS1115_COMP_3_GND -> compares 3 with GND - */ - void setCompareChannels(ADS1115_MUX mux); - - bool isBusy(); - void startSingleMeasurement(); - float getResult_V(); - float getResult_mV(); - - /* Get the raw result from the conversion register: - * The conversion register contains the conversion result of the amplified (!) - * voltage. This means the value depends on the voltage as well as on the - * voltage range. E.g. if the voltage range is 6.144 mV (ADS1115_RANGE_6144), - * +32767 is 6.144 mV; if the range is 4.096 mV, +32767 is 4.096 mV, and so on. - */ - int16_t getRawResult(); - - /* Scaling of the result to a different range: - * The results in the conversion register are in a range of -32767 to +32767 - * You might want to receive the result in a different scale, e.g. -1023 to 1023. - * For -1023 to 1023, and if you have chosen e.g. ADS1115_RANGE_4096, 0 Volt would - * give 0 as result and 4.096 mV would give 1023. -4.096 mV would give -1023. - */ - int16_t getResultWithRange(int16_t min, int16_t max); - - /* Scaling of the result to a different range plus scaling to a voltage range: - * You can use this variant if you also want to scale to a voltage range. E.g. in - * in order to get results equivalent to an Arduino UNO (10 bit, 5000 mV range), you - * would choose getResultWithRange(-1023, 1023, 5000). A difference to the Arduino - * UNO is that you can measure negative voltages. - * You have to ensure that the voltage range you scale to is smaller than the - * measuring voltage range. - */ - int16_t getResultWithRange(int16_t min, int16_t max, int16_t maxVoltage); - - /* This function returns the voltage range ADS1115_RANGE_XXXX in Millivolt */ - uint16_t getVoltageRange_mV(); - - /* With this function the alert pin will be active, when a conversion is ready. - * In order to deactivate, use the setAlertLimit_V function - */ - void setAlertPinToConversionReady(); - void clearAlert(); + ADS1115_WE(int addr); + ADS1115_WE(); // uses default I2C Address 0x48 + + void reset(); + bool init(); + + /* Set number of conversions after which the alert pin will be active + * - or you can disable the alert + * + * ADS1115_ASSERT_AFTER_1 -> after 1 conversion + * ADS1115_ASSERT_AFTER_2 -> after 2 conversions + * ADS1115_ASSERT_AFTER_4 -> after 4 conversions + * ADS1115_DISABLE_ALERT -> disable comparator // alert pin (default) + */ + void setAlertPinMode(ADS1115_COMP_QUE mode); + + /* Enable or disable latch. If latch is enabled the alarm pin will be active until the + * conversion register is read (getResult functions). If disabled the alarm pin will be + * deactivated with next value within limits. + * + * ADS1115_LATCH_DISABLED (default) + * ADS1115_LATCH_ENABLED + */ + void setAlertLatch(ADS1115_LATCH latch); + + /* Sets the alert pin polarity if active: + * + * Enable or disable latch. If latch is enabled the alarm pin will be active until the + * conversion register is read (getResult functions). If disabled the alarm pin will be + * deactivated with next value within limits. + * + * ADS1115_ACT_LOW -> active low (default) + * ADS1115_ACT_HIGH -> active high + */ + void setAlertPol(ADS1115_ALERT_POL polarity); + + /* Choose maximum limit or maximum and minimum alert limit (window)in Volt - alert pin will + * be active when measured values are beyond the maximum limit or outside the window + * Upper limit first: setAlertLimit_V(MODE, maximum, minimum) + * In max limit mode the minimum value is the limit where the alert pin will be deactivated (if + * not latched) + * + * ADS1115_MAX_LIMIT + * ADS1115_WINDOW + */ + void setAlertModeAndLimit_V(ADS1115_COMP_MODE mode, float hithres, float lothres); + + /* Set the conversion rate in SPS (samples per second) + * Options should be self-explaining: + * + * ADS1115_8_SPS + * ADS1115_16_SPS + * ADS1115_32_SPS + * ADS1115_64_SPS + * ADS1115_128_SPS (default) + * ADS1115_250_SPS + * ADS1115_475_SPS + * ADS1115_860_SPS + */ + void setConvRate(ADS1115_CONV_RATE rate); + + /* returns the conversion rate */ + convRate getConvRate(); + + /* Set continuous or single shot mode: + * + * ADS1115_CONTINUOUS -> continuous mode + * ADS1115_SINGLE -> single shot mode (default) + */ + void setMeasureMode(ADS1115_MEASURE_MODE mode); + + /* Set the voltage range of the ADC to adjust the gain: + * Please note that you must not apply more than VDD + 0.3V to the input pins! + * + * ADS1115_RANGE_6144 -> +/- 6144 mV + * ADS1115_RANGE_4096 -> +/- 4096 mV + * ADS1115_RANGE_2048 -> +/- 2048 mV (default) + * ADS1115_RANGE_1024 -> +/- 1024 mV + * ADS1115_RANGE_0512 -> +/- 512 mV + * ADS1115_RANGE_0256 -> +/- 256 mV + */ + void setVoltageRange_mV(ADS1115_RANGE range); + + /* Set the voltage range automatically + * 1) changes into maximum range and continuous mode + * 2) measures the voltage + * 3) chooses the smallest range in which the measured voltage is <80% + * of the range's maximum + * 4) switches back to single shot mode if it was in this mode before + * + * Please be aware that the procedure takes the the time needed for several conversions. + * You should ony use it in case you expect stable or slowly changing voltages. + */ + void setAutoRange(); + + /* Set the inputs to be compared + * + * ADS1115_COMP_0_1 -> compares 0 with 1 (default) + * ADS1115_COMP_0_3 -> compares 0 with 3 + * ADS1115_COMP_1_3 -> compares 1 with 3 + * ADS1115_COMP_2_3 -> compares 2 with 3 + * ADS1115_COMP_0_GND -> compares 0 with GND + * ADS1115_COMP_1_GND -> compares 1 with GND + * ADS1115_COMP_2_GND -> compares 2 with GND + * ADS1115_COMP_3_GND -> compares 3 with GND + */ + void setCompareChannels(ADS1115_MUX mux); + + bool isBusy(); + void startSingleMeasurement(); + float getResult_V(); + float getResult_mV(); + + /* Get the raw result from the conversion register: + * The conversion register contains the conversion result of the amplified (!) + * voltage. This means the value depends on the voltage as well as on the + * voltage range. E.g. if the voltage range is 6.144 mV (ADS1115_RANGE_6144), + * +32767 is 6.144 mV; if the range is 4.096 mV, +32767 is 4.096 mV, and so on. + */ + int16_t getRawResult(); + + /* Scaling of the result to a different range: + * The results in the conversion register are in a range of -32767 to +32767 + * You might want to receive the result in a different scale, e.g. -1023 to 1023. + * For -1023 to 1023, and if you have chosen e.g. ADS1115_RANGE_4096, 0 Volt would + * give 0 as result and 4.096 mV would give 1023. -4.096 mV would give -1023. + */ + int16_t getResultWithRange(int16_t min, int16_t max); + + /* Scaling of the result to a different range plus scaling to a voltage range: + * You can use this variant if you also want to scale to a voltage range. E.g. in + * in order to get results equivalent to an Arduino UNO (10 bit, 5000 mV range), you + * would choose getResultWithRange(-1023, 1023, 5000). A difference to the Arduino + * UNO is that you can measure negative voltages. + * You have to ensure that the voltage range you scale to is smaller than the + * measuring voltage range. + */ + int16_t getResultWithRange(int16_t min, int16_t max, int16_t maxVoltage); + + /* This function returns the voltage range ADS1115_RANGE_XXXX in Millivolt */ + uint16_t getVoltageRange_mV(); + + /* With this function the alert pin will be active, when a conversion is ready. + * In order to deactivate, use the setAlertLimit_V function + */ + void setAlertPinToConversionReady(); + void clearAlert(); private: - uint16_t voltageRange; - ADS1115_MEASURE_MODE deviceMeasureMode; - int i2cAddress; - int16_t calcLimit(float rawLimit); - uint8_t writeRegister(uint8_t reg, uint16_t val); - uint16_t readRegister(uint8_t reg); + uint16_t voltageRange; + ADS1115_MEASURE_MODE deviceMeasureMode; + int i2cAddress; + void delayAccToRate(convRate cr); + int16_t calcLimit(float rawLimit); + uint8_t writeRegister(uint8_t reg, uint16_t val); + uint16_t readRegister(uint8_t reg); }; -- cgit v1.2.3